﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SchoolLibrary.Entity.Person;
using SchoolLibrary.Entity;
using SchoolLibrary.Utils;
using SchoolLibrary.DataBase.DAO;
using SchoolLibrary.Desktop.Guide.Input;

namespace SchoolLibrary.Desktop.Guide
{
    public class PupilGuide : GuideForm<Pupil>
    {
        private string[] COLUMN_NAMES = {"id", "Имя", "Фамилия", "Класс"};

        public PupilGuide() { }

        public PupilGuide(ViewModeEnum mode) : base(mode) { }

        protected override string[] getColumnNames() 
        {
            return COLUMN_NAMES;
        }

        protected override object[] createRow(Pupil pupil)
        {
            List<object> row = new List<object>();
            row.Add(pupil.Id);
            row.Add(pupil.Name);
            row.Add(pupil.Surname);
            Class pupilClass = pupil.PupilClass;
            row.Add(pupilClass.Number + StringUtil.SPACE +  pupilClass.Letter);
            return row.ToArray();
        }

        protected override List<Pupil> findAll()
        {
            return PupilDAO.getInstance().findAll();
        }

        protected override Pupil findById(long id)
        {
            return PupilDAO.getInstance().findById(id);
        }

        protected override void addButton_Click(object sender, EventArgs e)
        {
            PupilInputDialog dialog = new PupilInputDialog();
            dialog.ShowDialog();
            Pupil pupil = dialog.Pupil;
            if (pupil != null)
            {
                PupilDAO.getInstance().save(pupil);
            }
            this.initData();
        }

        protected override void editButton_Click(object sender, EventArgs e)
        {
            long id = getSelectedRowId();
            Pupil selectedPupil = PupilDAO.getInstance().findById(id);
            PupilInputDialog dialog = new PupilInputDialog(selectedPupil);
            dialog.ShowDialog();
            Pupil pupil = dialog.Pupil;           
            PupilDAO.getInstance().save(pupil);  
            this.initData();
        }

        protected override void deleteButton_Click(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }


    }
}
